package org.pjlib.net;

import com.blankj.utilcode.util.StringUtils;
import com.lzy.okgo.model.HttpParams;

import org.pjlib.common.AppKey;

import java.io.File;
import java.util.Map;

/**
 * Created by ws on 2019/04/15.
 */
public abstract class BaseApi {
    public static String appKey="";
    public static void get(String url, int rCode, JsonCallBack callBack) {
        get(url,null,rCode, callBack);
    }
    public static void post(String url, int rCode, JsonCallBack callBack){
        post(url,null,rCode,callBack);
    }
    public static void post(String url, Map<String,Object> map, int rCode, JsonCallBack callBack){
        HttpParams params=getKeyParams();
        if(map!=null){
            for (String key:map.keySet()
                 ) {
                Object obj=map.get(key);
                if(obj instanceof Integer){
                    params.put(key,(int)obj);
                }else if(obj instanceof String){
                    params.put(key,(String) obj);
                }else if(obj instanceof File){
                    params.put(key,(File) obj);
                }else if(obj instanceof Double){
                    params.put(key,(double)obj);
                }else if(obj instanceof Float){
                    params.put(key,(float)obj);
                }else {
                    if(obj!=null){
                        params.put(key,String.valueOf(obj));
                    }
                }
            }
        }
        HttpUtils.post(new RequestCall(url, rCode,params), callBack);
    }
    public static void get(String url, Map<String,Object> map, int rCode, JsonCallBack callBack){
        HttpParams params=getKeyParams();
        if(map!=null){
            for (String key:map.keySet()
            ) {
                Object obj=map.get(key);
                if(obj instanceof Integer){
                    params.put(key,(int)obj);
                }else if(obj instanceof String){
                    params.put(key,(String) obj);
                }else if(obj instanceof File){
                    params.put(key,(File) obj);
                }
            }
        }
        HttpUtils.get(new RequestCall(url, rCode,params), callBack);
    }
    public static HttpParams getKeyParams(){
        HttpParams params=new HttpParams();
        if(!StringUtils.isEmpty(appKey)){
            params.put("appKey",appKey);
        }else {
            appKey= AppKey.getAppKey();
            if(!StringUtils.isEmpty(appKey)){
                params.put("appKey",appKey);
            }
        }
        return params;
    }
}

